import {historyPush} from "./index"
import toast from "./toast";
// import qiniuUploader from "@/utils/sdk/qiniuUploader";
import store from '../store'


/*  #ifdef  MP-WEIXIN || APP-PLUS */
const baseUrl = 'https://zgh.hylxzcb.com';
// const baseUrl = 'http://192.168.0.180/';



/*  #endif  */
/*  #ifdef  H5  */
const baseUrl = '';

// uni.setStorageSync('token','eb7d176cebb1f27153e1f6972a8bc3dc')
/*  #endif  */
const commonSuccessFunction= function (res,needRemind=true) {
  if(res&&res.code){
    res.code = Number(res.code)

    if(res.code===400){
      res.code=200;
      res.data=[]
    }
    if(res.msg==='未查询到数据！'){
      res.code=200;
      res.data=[]
      toast.info(res.msg)
    }
    if(res.code===102){ //登录失效
      store.dispatch('logout');
      historyPush('my');
    }
    if(res.code!==200 ){
      needRemind&&toast.info(res.msg)
    }
    return res;
  }
}
/**
 * errorRemind:是否显示错误提醒
 * loading:显示loading
 * */
/*  #ifdef   APP-PLUS */
// uni.setStorageSync('token','eb7d176cebb1f27153e1f6972a8bc3dc')
uni.setStorageSync('token','be77080b84022582c23a92039ffeafb3')
/*  #endif  */
export default function Api(url, data={}, loading={mask:false},needRemind=true) {
  const requestUrl = baseUrl+'/api/'+url;


  const token = uni.getStorageSync('token')
  return new Promise((resolve, reject) => {
    if(loading){
      toast.loading(undefined,!!loading.mask)
    }
    uni.request({
      url: requestUrl,
      data: {
        ...data,
        token:token
      },
      method:'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if(loading){
          toast.hide()
        }
        try{
          if(res.statusCode===200){
            resolve(commonSuccessFunction(res.data,needRemind))
          }else{
            needRemind&&toast.info('请求失败')
          }
        }catch (e) {

        }
      },
      fail:function (err) {
        needRemind&&toast.info('请求失败')
        reject(err)
      }
    })
  })
}


/**
 *
 * tempFilePaths: 文件暂存列表
 * type 1: 图片
 * type 2: 音频
 * type 3: 视频
 *
 * */
/**
 *
 * tempFilePaths: 文件暂存列表
 * type 1: 图片
 * type 2: 音频
 * type 3: 视频
 *
 * */
export async function uploadFile(FilePath) {
  let uploadToken = await store.initUploadToken();
  const options = {
    uploadURL: 'https://up-z2.qiniup.com',
    region: 'SCN', // 华南区
    uptoken:uploadToken,
    // domain: 'https://adn.xsdyiwu.com',
    shouldUseQiniuFileName: true
  };
  // qiniuUploader.init(options);

  // return new Promise((resolve,reject) => {
  //   let uploadTask
  //   uploadTask =qiniuUploader.upload(FilePath, async (res) => {
  //     let bool = await cloud.checkImage('https://cdn.hylxzcb.com/' + res.key);
  //     if(bool){
  //       resolve(res)
  //     }else{
  //       reject({
  //         error:true
  //       })
  //     }
  //
  //   }, (error) => {
  //     if(uploadTask&&uploadTask.abort){
  //       uploadTask.abort()
  //     }
  //     reject(error)
  //   });
  //
  // })
}

